package com.csglxt.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.csglxt.base.DaoSupportImpl;
import com.csglxt.domain.Goods;
import com.csglxt.domain.GoodsCate;
import com.csglxt.service.GoodsService;

/**
 * 
 * @ClassName: GoodsServiceImpl
 * @Description: TODO(商品管理业务层实现类)
 * @author Itchbeilef
 * @date 2015-5-28 下午8:56:54
 * 
 */
@Service
@Transactional
public class GoodsServiceImpl extends DaoSupportImpl<Goods> implements
        GoodsService {
    /**
     * 根据商品分类查询商品列表
     */
    @SuppressWarnings("unchecked")
    public List<Goods> queryGoodsListByCate(GoodsCate goodsCate) {
        return getSession().createQuery("FROM Goods g WHERE g.goodsCate = ?")//
                .setParameter(0, goodsCate)//
                .list();

    }

    /**
     * 根据商品分类列表查询商品列表
     */
    @SuppressWarnings("unchecked")
    public List<Goods> queryGoodsListByCateList(List<GoodsCate> goodsCateList) {
        return getSession()
                .createQuery(
                        "FROM Goods g WHERE g.goodsCate in (:goodsCateList)")//
                .setParameterList("goodsCateList", goodsCateList)//
                .list();
    }

    /**
     * 根据商品名称模糊查询商品列表
     */
    @SuppressWarnings("unchecked")
    public List<Goods> queryGoodsListByName(String goodsName) {
        return getSession().createQuery("FROM Goods g WHERE g.name like ?")//
                .setParameter(0, "%" + goodsName + "%")//
                .list();
    }

    /**
     * ajax验证
     */
    public Goods findByname(Goods goods) {
        return (Goods) getSession().createQuery(//
                "FROM Goods g WHERE g.name = ? and g.goodsCate.id = ?")
                //
                .setParameter(0, goods.getName())
                .setParameter(1, goods.getGoodsCate().getId())//
                .uniqueResult();
    }

}
